﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using DataAccessLayer.DAL;
using System.Data.Objects;
using Common;

namespace BestBuy.UserViews
{
    public partial class ManagerAuctions : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            User user = UserDAL.GetUserByUsername(HttpContext.Current.User.Identity.Name);
            if (user == null || user.RoleID != (int)RoleType.Seller)
            {
                Response.Redirect("~/Account/Login.aspx");
            }
            if (!IsPostBack)
            {
                LoadBookmark(user);
                LoadJoinAuctions(user);
                LoadWonAuctions(user);
            }
        }
        
        private void LoadBookmark(User user)
        {
            User userBookmark = UserDAL.GetAllBookmark(user.ID);
            gvBookmark.DataSource = userBookmark.FollowAuctions;
            gvBookmark.DataBind();
        }

        private void LoadJoinAuctions(User user)
        {
            List<Auction> auctions = SellerDAL.GetAllJoinAuction(user.ID);
            gvJoinAuctions.DataSource = auctions;
            gvJoinAuctions.DataBind();
        }

        private void LoadWonAuctions(User user)
        {
            User WonAuctions = SellerDAL.GetWonAuctions(user.ID);
            gvWonAuctions.DataSource = WonAuctions.AuctionTransactions;
            gvWonAuctions.DataBind();
        }

        protected void gvBookmark_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataAccessLayer.Auction auc = (DataAccessLayer.Auction)DataBinder.Eval(e.Row.DataItem, "Auction");
                if (auc != null)
                {
                    HyperLink link = e.Row.FindControl("AuctionLink") as HyperLink;
                    Label endtime = e.Row.FindControl("EndTimeLable") as Label;
                    if (link != null && endtime != null)
                    {
                        link.Text = auc.Title;
                        link.NavigateUrl = string.Format("~/UserViews/SellerBid.aspx?AuctionID={0}", auc.ID);
                        endtime.Text = auc.EndTime.ToString();
                    }
                }
            }
            
        }

        protected void gvJoinAuctions_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        }

        protected void gvWonAuctions_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataAccessLayer.Auction auc = (DataAccessLayer.Auction)DataBinder.Eval(e.Row.DataItem, "Auction");
                int status = (int)DataBinder.Eval(e.Row.DataItem,"Status");
                if (auc != null)
                {
                    HyperLink Auctionlink = e.Row.FindControl("AuctionLink_Won") as HyperLink;
                    HyperLink Feedbacklink = e.Row.FindControl("FeedbackLink") as HyperLink;
                    Label lbstatus = e.Row.FindControl("StatusLable_won") as Label;
                    if (Auctionlink != null && Feedbacklink != null && lbstatus != null)
                    {
                        Auctionlink.Text = auc.Title;
                        Auctionlink.NavigateUrl = string.Format("~/UserViews/SellerBid.aspx?AuctionID={0}", auc.ID);
                        Feedbacklink.Text = "Feedback";
                        Feedbacklink.NavigateUrl = string.Format("~/UserViews/Feedback.aspx?AuctionID={0}",auc.ID);
                        lbstatus.Text = Auc_Tran_Status(status);
                    }
                }
            }
        }
        private string Auc_Tran_Status(int status)
        {
            string result = "";
            if (status == (int)AuctionTransactionStatus.Fail)
                result = AuctionTransactionStatus.Fail.ToString();
            else if (status == (int)AuctionTransactionStatus.Success)
                result = AuctionTransactionStatus.Success.ToString();
            else if (status == (int)AuctionTransactionStatus.Waitting)
                result = AuctionTransactionStatus.Waitting.ToString();

            return result;
        }
    }
}